﻿Imports System.Windows.Forms
Imports System.Drawing.Drawing2D
Imports System.Drawing
Imports System.Media

Public Class CTEditorForm

    Public Sub New(ByRef prop As ContourProp)

        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。
        mainList.Clear()
        lstContour.Items.Clear()

        cprop = prop
        For Each t As ContourSub In cprop.CListProperty
           

            mainList.Add(t.VProperty, t)
            lstContour.Items.Add(t.VProperty.ToString())


            '都是颜色计算惹的祸啊！
            Dim temp As Double = t.VProperty
            If temp < minVal Then

                minVal = temp

            End If

            If temp > maxVal Then
                maxVal = temp

            End If


        Next



    End Sub


    'Dim mainList As New List(Of ContourSub)
    Dim mainList As New Dictionary(Of Double, ContourSub)

    Dim nCounts As Integer = 0
    Dim minVal As Double = Double.MaxValue
    Dim maxVal As Double = Double.MinValue




    Dim cprop As ContourProp


    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click

        Dim temp As New List(Of ContourSub)

        'cprop.CListProperty.Clear()
        For Each p As KeyValuePair(Of Double, ContourSub) In mainList
            Dim val As ContourSub = p.Value

            'cprop.CListProperty.Add(val)
            temp.Add(val)

        Next

        cprop.CListProperty = temp


        Me.Close()

    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompute.Click
        Dim str As String = txtSelf.Text.Trim()

        Dim lst As New List(Of String)

        Dim nstart As Integer = -1
        Dim nend As Integer = -1

        If str = "" Then
            Return
        End If






        Dim partStr As String

        nstart = 0

        While str.IndexOf(",") <> -1

            nend = str.IndexOf(",")
            partStr = str.Substring(nstart, nend)


            If lst.Contains(partStr) Then

            Else

                If partStr = "" Then
                Else
                    lst.Add(partStr)
                End If

            End If



            nend = nend + 1
            Dim strTemp As String
            Dim nlength As Integer = str.Length - 1

            strTemp = str.Substring(nend)
            str = strTemp

        End While


        '加入最后一次
        nend = str.Length
        partStr = str.Substring(nstart, nend)

        If lst.Contains(partStr) Then

        Else

            If partStr = "" Then
            Else
                lst.Add(partStr)
            End If

        End If



        '排序和去重复
        lst.Sort()




        '清空前次的线条
        lstContour.Items.Clear()
        mainList.Clear()

        '为了讨厌的颜色逻辑，真是可恶，和我想的不一样，也得改啊
        nCounts = lst.Count
        minVal = lst.Item(0)
        maxVal = lst.Item(nCounts - 1)

        For Each s As String In lst

            lstContour.Items.Add(s)
            Me.addContourSub(Double.Parse(s))

        Next

     


    End Sub

    Private Sub btnCompute2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompute2.Click
        If txtMax.Text = "" Or txtMin.Text = "" Or txtSpace.Text = "" Then
            Return
        End If

        If txtMax.ToString() < txtMin.ToString() Then
            MessageBox.Show("最大值应该大于最小值")
            Return
        End If

        Dim min As Double = Double.Parse(txtMin.Text)
        Dim max As Double = Double.Parse(txtMax.Text)

        Dim space As Double = Double.Parse(txtSpace.Text)

        If (space > max - min) Then

            MessageBox.Show("间距值过大")
            Return
        End If



        lstContour.Items.Clear()
        mainList.Clear()


        '为了讨厌的颜色逻辑，真是可恶，和我想的不一样，也得改啊
        nCounts = 0
        For i As Double = min To max Step space
            nCounts = nCounts + 1
        Next
        minVal = min
        maxVal = max


        For i As Double = min To max Step space

            lstContour.Items.Add(i.ToString())
            Me.addContourSub(i)
        Next


    End Sub

    Private Sub txtSelf_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSelf.TextChanged

    End Sub

    Private Sub lstContour_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstContour.Click

    End Sub

    Private Sub lstContour_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstContour.SelectedIndexChanged
        propSub.SelectedObject = mainList.Item(lstContour.SelectedItem)
    End Sub

    Private Sub addContourSub(ByVal val As Double)

      

        If mainList.ContainsKey(val) Then

            MessageBox.Show("值为" + val.ToString() + "的线已经存在！")
            Return
        Else

            Dim c As New ContourSub
            c.LnColorProperty = Me.GetLineColor(val)
            c.VProperty = val
            c.ClrProperty = Me.GetPolygonColor(val)
            mainList.Add(val, c)

        End If




    End Sub

    Private Function GetLineColor(ByVal val As Double) As Color

        Dim n As Integer = cprop.LineGradientListProperty.Count

        If n < 1 Then
            Return Nothing
        End If

        Dim clrTemp As New List(Of Color)

        If n > 1 Then
            Dim a As Color
            Dim b As Color

            Dim temp As Color

            For i As Integer = 0 To n - 2
                a = cprop.LineGradientListProperty.Item(i)
                b = cprop.LineGradientListProperty.Item(i + 1)

                For j As Double = 1 To 1000

                    Dim atempr As Double = a.R
                    Dim atempg As Double = a.G
                    Dim atempb As Double = a.B

                    Dim btempr As Double = b.R
                    Dim btempg As Double = b.G
                    Dim btempb As Double = b.B


                    Dim rc As Double = atempr + (btempr - atempr) * j / 1000D
                    Dim gc As Double = atempg + (btempg - atempg) * j / 1000D
                    Dim bc As Double = atempb + (btempb - atempb) * j / 1000D

                    temp = Color.FromArgb(rc, gc, bc)

                    clrTemp.Add(temp)


                Next


            Next

        Else
            clrTemp.Add(cprop.LineGradientListProperty.Item(0))


        End If

        Dim nAll As Integer = clrTemp.Count


        Dim index As Integer

        If nAll = 1 Then
            index = 0
        Else

            If (minVal <> maxVal) Then
                index = nAll * (val - minVal) / (maxVal - minVal) - 1
            Else
                index = 0
            End If


        End If

        If index > nAll - 1 Then
            index = nAll - 1
        End If

        If index < 0 Then
            index = 0
        End If
       


        Return clrTemp.Item(index)




    End Function

    Private Function GetPolygonColor(ByVal val As Double) As Color


        Dim n As Integer = cprop.PolyGradientListProperty.Count

        If n < 1 Then
            Return Nothing
        End If

        Dim clrTemp As New List(Of Color)

        If n > 1 Then
            Dim a As Color
            Dim b As Color

            Dim temp As Color

            For i As Integer = 0 To n - 2
                a = cprop.PolyGradientListProperty.Item(i)
                b = cprop.PolyGradientListProperty.Item(i + 1)

                For j As Double = 1 To 1000

                    Dim atempr As Double = a.R
                    Dim atempg As Double = a.G
                    Dim atempb As Double = a.B

                    Dim btempr As Double = b.R
                    Dim btempg As Double = b.G
                    Dim btempb As Double = b.B


                    Dim rc As Double = atempr + (btempr - atempr) * j / 1000D
                    Dim gc As Double = atempg + (btempg - atempg) * j / 1000D
                    Dim bc As Double = atempb + (btempb - atempb) * j / 1000D

                    temp = Color.FromArgb(rc, gc, bc)

                    clrTemp.Add(temp)


                Next


            Next

        Else
            clrTemp.Add(cprop.PolyGradientListProperty.Item(0))


        End If

        Dim nAll As Integer = clrTemp.Count

        '颜色的返回逻辑，我也很郁闷
        Dim index As Integer


        If nAll = 1 Then
            index = 0
        Else

            If (minVal <> maxVal) Then
                index = nAll * (val - minVal) / (maxVal - minVal) - 1
            Else
                index = 0
            End If


        End If

        If index > nAll - 1 Then
            index = nAll - 1
        End If

        If index < 0 Then
            index = 0
        End If

        Return clrTemp.Item(index)


    End Function


    Private Sub txtMin_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtMin.KeyPress
        


        Select Case e.KeyChar
            Case "0" To "9"
            Case "."
            Case ""
            Case Else
                e.Handled = True

        End Select


    End Sub

    Private Sub txtMax_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtMax.KeyPress
        Select Case e.KeyChar
            Case "0" To "9"
            Case "."
            Case ""
            Case Else
                e.Handled = True

        End Select
    End Sub

    Private Sub txtSpace_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSpace.KeyPress
        Select Case e.KeyChar
            Case "0" To "9"
            Case "."
            Case ""
            Case Else
                e.Handled = True

        End Select
    End Sub

    Private Sub txtSelf_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSelf.KeyPress
        Select Case e.KeyChar
            Case "0" To "9"
            Case "."
            Case ""
            Case ","


            Case Else
                e.Handled = True

        End Select
    End Sub

    Private Sub btnLineClr_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLineClr.Click

        For Each p As KeyValuePair(Of Double, ContourSub) In mainList
            Dim k As Double = p.Key
            Dim v As ContourSub = p.Value

            v.LnColorProperty = Me.GetLineColor(k)


        Next

    End Sub

    Private Sub btnPolyClr_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPolyClr.Click

        For Each p As KeyValuePair(Of Double, ContourSub) In mainList
            Dim k As Double = p.Key
            Dim v As ContourSub = p.Value

            v.ClrProperty = Me.GetPolygonColor(k)


        Next
    End Sub

End Class